﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_Profile_AuthenticateEmail : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnResendEmail.Click += new EventHandler(btnResendEmail_Click);
        if (!IsPostBack)
        {
            string email = Request.QueryString["Email"];
            Acms.Bll.User user = Acms.Bll.User.GetByEmailAddress(email, AcmsContext.SiteId);
            if (user != null)
            {
                lblEmail.Text = user.EmailAddress;
                lblName.Text = user.DisplayName;
                pnlFound.Visible = true;
                pnlNotFound.Visible = false;
            }
            else
            {
                lblEmailNotFound.Text = email;
                pnlFound.Visible = false;
                pnlNotFound.Visible = true;
            }
        }
    }

    void btnResendEmail_Click(object sender, EventArgs e)
    {
        string email = Request.QueryString["Email"];
        Acms.Bll.User user = Acms.Bll.User.GetByEmailAddress(email, AcmsContext.SiteId);
        if (user != null)
        {
            var code = Acms.Bll.EmailActivationCode.GetAllByUserIdAndEmail(user.Id, email).Where(x => x.Activated == null).FirstOrDefault();
            if (code == null)
            {
                code = Acms.Bll.EmailActivationCode.CreateInstance(user.Id, email, AcmsContext.SiteId);
                code.Update();
            }
            code.Send(Server.MapPath("~/Sites/Man/Emails/AuthenticateEmail.xml"), AcmsContext.SiteRoot, AcmsContext.GetUrl("~/Profile/ActivateEmail.aspx"));
        }
        
    }

    void btnTemp_Click(object sender, EventArgs e)
    {
        string email = Request.QueryString["Email"];
        Acms.Bll.User user = Acms.Bll.User.GetByEmailAddress(email, AcmsContext.SiteId);
        if (user != null)
        {
            var code = Acms.Bll.EmailActivationCode.GetAllByUserIdAndEmail(user.Id, email).Where(x => x.Activated == null).FirstOrDefault();
            if(code == null)
            {
                code = Acms.Bll.EmailActivationCode.CreateInstance(user.Id, email, AcmsContext.SiteId);
                code.Update();
            }
            if (code != null)
            {
                Response.Redirect(string.Format("ActivateEmail.aspx?Code={0}", Server.UrlEncode(code.Code)));
            }
        }
    }
}
